package com.oocl.o2o.admin.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.oocl.o2o.admin.controller.AdvertisementController;
import com.oocl.o2o.admin.dao.AdvertisementDao;
import com.oocl.o2o.admin.dao.MerchantDao;
import com.oocl.o2o.admin.entity.Advertisement;
import com.oocl.o2o.admin.entity.Merchant;
import com.oocl.o2o.admin.service.AdvertisementService;
import com.oocl.o2o.admin.util.Base64Unit;
import com.oocl.o2o.admin.util.FileUtil;
import com.oocl.o2o.admin.util.ImageUtil;
import com.oocl.o2o.admin.vo.AdvertisementVO;
import com.oocl.o2o.admin.vo.MerchantVO;
import com.oocl.o2o.connectutil.dto.AdvertFeedbackToMDTO;
import com.oocl.o2o.connectutil.dto.AdvertisementDTO;
import com.oocl.o2o.connectutil.dto.Status;
import com.oocl.o2o.connectutil.util.AdvertForAUtil;

@Service(value="advertisementServiceImpl")
public class AdvertisementServiceImpl implements AdvertisementService{

	private static Logger logger = Logger.getLogger(AdvertisementServiceImpl.class);
	
	@Resource(name="advertisementDaoImpl")
	private AdvertisementDao advertisementDao;
	
	@Resource(name="merchantDaoImpl")
	private MerchantDao merchantDao;
	
	@Override
	@Transactional
	public Advertisement add(Advertisement advertisement) {
		// TODO Auto-generated method stub
		return advertisementDao.add(advertisement);
	}

	@Override
	@Transactional
	public Advertisement delete(String id) {
		// TODO Auto-generated method stub
		return advertisementDao.delete(id);
	}
	

	@Override
	@Transactional
	public Advertisement update(Advertisement advertisement) {
		// TODO Auto-generated method stub
		return advertisementDao.update(advertisement);
	}

	@Override
	public List<Advertisement> findByApprovalStatus(int approvalStatus) {
		// TODO Auto-generated method stub
		return advertisementDao.findByApprovalStatus(approvalStatus);
	}

	@Override
	public List<Advertisement> findAll() {
		// TODO Auto-generated method stub
		return advertisementDao.findAll();
	}

	@Override
	public List<Advertisement> findByMerchantId(String id) {
		// TODO Auto-generated method stub
		return advertisementDao.findByMerchantId(id);
	}

	@Override
	@Transactional
	public Advertisement applyAdvertisement(AdvertisementDTO advertisementDTO) {
		// TODO Auto-generated method stub
		logger.info("获取M端所发送的广告dto");
		Advertisement advertisement = new Advertisement();
		Merchant merchant = merchantDao.findByName(advertisementDTO.getMerchantName());

		if(merchant!=null){

		byte[] foodImage = Base64Unit.decode(advertisementDTO.getFoodImage());

		advertisement.setMerchant(merchant);
		advertisement.setFoodId(advertisementDTO.getAdvertId());
		advertisement.setFoodName(advertisementDTO.getFoodName());
		advertisement.setFoodImage(foodImage);
		advertisement.setFoodPrice(advertisementDTO.getFoodPrice());
		advertisement.setTenderPrice(advertisementDTO.getTenderPrice());
		advertisement.setFoodType(advertisementDTO.getFoodType());
		advertisement.setApplyReason(advertisementDTO.getApplyReason());
		//advertisement.setFoodId(advertisementDTO.getAdvertId());
		
		
		 advertisementDao.add(advertisement);
		 logger.info("完成数据库的插入");
		// 写入路径
		try {
		FileUtil.writeFile(foodImage, ImageUtil.getImagePath(advertisement.getId(), "advert"));
		} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		}
		return advertisement;

		}else{
		logger.error("广告申请商家异常");
		return null;
		}
	}

	@Override
	public Advertisement findAdvertisementById(String id) {
		
		return advertisementDao.findById(id);
		
	}

	/**
	 * 同步封装使用
	 * @param id
	 * @return
	 */
	@Override
	@Transactional
	public AdvertisementVO findByIdForDetail(String id) {
		// TODO Auto-generated method stub
		logger.info("进入方法 find by id for detail "+id);
		Advertisement advertisement=advertisementDao.findById(id);
//		String idCardSrc=ImageUtil.getImagePath(merchant.getUname(),"id");
		AdvertisementVO advertisementVO=new AdvertisementVO();
		advertisementVO.setId(id);
		advertisementVO.setMerchantId(advertisement.getMerchant().getId());
		advertisementVO.setMerchantUname(advertisement.getMerchant().getUname());
		advertisementVO.setApplyContent(advertisement.getApplyReason());
		advertisementVO.setFoodType(advertisement.getFoodType());
		advertisementVO.setFoodName(advertisement.getFoodName());
		advertisementVO.setFoodPrice(advertisement.getFoodPrice());
		advertisementVO.setTenderPrice(advertisement.getTenderPrice());
		advertisementVO.setApprovalStatus(advertisement.getApprovalStatus());
		logger.info("返回上层的vo");
		return advertisementVO;
	}

	@Override
	public String findByIdForShowImage(String id, String type) {
		// TODO Auto-generated method stub
		logger.info("开始读取文件显示在页面");
		Advertisement advertisement=advertisementDao.findById(id);
		String filePath=ImageUtil.getImagePath(advertisement.getId(), type);
		return filePath;
	}


	@Override
	@Transactional
	public Advertisement updateStatusAndSend(AdvertisementVO advertisementVO) {
		// TODO Auto-generated method stub
		logger.info("开始准备更新广告的状态");
		Advertisement advertisement=advertisementDao.findById(advertisementVO.getId());
		
		advertisement.setApprovalStatus(advertisementVO.getApprovalStatus());
		Advertisement advert=advertisementDao.update(advertisement);
		logger.info("A 端数据库已经同步");
		AdvertFeedbackToMDTO advertFeedbackToMDTO=null;
		if (advertisementVO.getApprovalStatus()==1) {
			logger.info("状态为1,广告通过,准备发送至M端");
			advertFeedbackToMDTO=new AdvertFeedbackToMDTO(advert.getFoodId(),Status.OPEN);
		}
		if (advertisementVO.getApprovalStatus()==2) {
			logger.info("状态为2,广告移除,准备发送至M端");
			advertFeedbackToMDTO=new AdvertFeedbackToMDTO(advert.getFoodId(),Status.OFF);
		}
		AdvertForAUtil.send(advertFeedbackToMDTO);
		logger.info("已发送");
		return advert;
	}
	
	
	


}
